package cao.oschina.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间
 * <p/>
 * Created by Haidy on 15/3/28.
 */
public class TimeUtil {

    /**
     * 友好时间
     *
     * @param time time
     * @return time
     */
    public static String friendTime(String time) {
        Date date = hd.util.TimeUtil.toDate(time);
        Calendar calendar = Calendar.getInstance();
        assert date != null;
        //判断是否同一天
        if (hd.util.TimeUtil.isSameDay(date)) {
            int hour = (int) ((calendar.getTimeInMillis() - date.getTime()) / 3600000);
            if (hour == 0) {
                long minute = Math.max((calendar.getTimeInMillis() - date.getTime()) / 60000, 0);
                if (minute == 0) {
                    time = "刚刚";
                } else {
                    time = minute + "分钟前";
                }
            } else {
                time = hour + "小时前";
            }
            return time;
        }
        long lt = date.getTime() / 86400000;
        long ct = calendar.getTimeInMillis() / 86400000;
        int days = (int) (ct - lt);
        if (days == 0) {
            int hour = (int) ((calendar.getTimeInMillis() - date.getTime()) / 3600000);
            if (hour == 0) {
                long minute = Math.max((calendar.getTimeInMillis() - date.getTime()) / 60000, 0);
                if (minute == 0) {
                    time = "刚刚";
                } else {
                    time = minute + "分钟前";
                }
            } else {
                time = hour + "小时前";
            }
            return time;
        } else if (days == 1) {
            return "昨天";
        } else if (days == 2) {
            return "前天";
        } else if (days < 10) {
            return days + "天前";
        }
        return hd.util.TimeUtil.toString("yyyy-MM-dd", date);
    }

}
